# backend/models/user.py
from datetime import datetime
from sqlalchemy import Boolean, String, Integer, DateTime, ForeignKey, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from backend.db.base import Base
import uuid


class User(Base):
    __tablename__ = "users"

    id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
    email: Mapped[str] = mapped_column(String(255), unique=True, index=True)
    hashed_password: Mapped[str] = mapped_column(String(255))
    full_name: Mapped[str] = mapped_column(String(100), nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    is_vip: Mapped[bool] = mapped_column(Boolean, default=False)
    total_traffic: Mapped[int] = mapped_column(Integer, default=0)
    used_traffic: Mapped[int] = mapped_column(Integer, default=0)
    invite_code: Mapped[str] = mapped_column(String(32), unique=True, nullable=True)
    inviter_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    # 关系
    inviter = relationship("User", remote_side=[id])
    user_subscriptions = relationship("UserSubscription", back_populates="user")